package com.cabinet.db;

import com.cabinet.Config;

import javax.swing.table.AbstractTableModel;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayWriter;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: denny
 * Date: 9/24/13
 * Time: 11:36 AM
 * To change this template use File | Settings | File Templates.
 */
@XmlRootElement(name = "classes")
public class DBClasses {
  static DBClasses inst = new DBClasses();
  
  List<DBClass> list = new ArrayList<>();
  TabModel tabModel = new TabModel();

  public static DBClasses inst(){
    return inst;
  }
  
  @XmlElement(name = "class")
  public List<DBClass> getList() {
    return list;
  }

  public void setList(List<DBClass> list) {
    this.list = list;
  }

  public AbstractTableModel getTableModel() {
    return tabModel;
  }
  
  public static void loadFromDiskDB() throws Exception{
    File dbFile = Config.classesDB();
    if( dbFile.canRead() ){
      JAXBContext jc = JAXBContext.newInstance(DBClasses.class);
      inst =  (DBClasses)jc.createUnmarshaller().unmarshal(dbFile);
    }
  }

  public void save() throws Exception{
    JAXBContext jc = JAXBContext.newInstance(DBClasses.class);
    Marshaller jaxbMarshaller = jc.createMarshaller();
    // output pretty printed
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    jaxbMarshaller.marshal(this, baos);
    byte[] newXML = baos.toByteArray();

    if( Config.classesDB().canRead() ){
      byte[] origFile = Files.readAllBytes(Config.classesDB().toPath());
      if( Arrays.equals(origFile, newXML) ){
        return; // nothing changed
      }
      Config.classesDB().renameTo(Config.classesDBBackup());
    }
    Files.write(Config.classesDB().toPath(), newXML);
  }

  public DBClass getClass(int row){
    return list.get(row);
  }

  public void addClass(){
    DBClass c = new DBClass();
    list.add(c);
    c.setName("Клас "+list.size());
    tabModel.fireTableRowsInserted(list.size() - 1, list.size() - 1);
  }
  
  class TabModel extends AbstractTableModel{
    @Override
    public String getColumnName(int column) {
      return "Класи";
    }

    @Override
    public int getRowCount() {
      return list.size();
    }

    @Override
    public int getColumnCount() {
      return 1;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
      return list.get(rowIndex).getName();
    }

    public boolean isCellEditable(int row, int column) {
      return true;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
      list.get(rowIndex).setName((String)aValue);
    }
  }
}
